const express = require('express');
const router = express.Router();
// let db = require("../mysql/db")
const userInfo = require('../source/baseInfo');


/**
 * POST /api/v1/saveClassroom
 * @summary 添加教室
 * @description 添加教室
 * @tags [教室管理]
 * @produces application/json
 * @param {object} request.body.required - 教室信息
 * @return {object} 201 - 添加教室ok
 * @example request - 示例请求
 * {
 *    "name": "1324",
 *    "jxl": "北校区1号教学楼",
 *    "xq": "北校区",
 *    "jssbpz": "教室设备配置",
 *    "rl": "40"
 * }
 * @example response - 示例响应
 * {
 *   "data": 'ok'
 * }
 * @consumes application/json
 * @return {array<object>} 200 - 成功添加教室
 */
router.post('/api/v1/saveClassroom', async (req, res, next) => {
  const { id, name, jxl, xq, jssbpz, rl } = req.body;
  const fs = require('fs');
  const path = require('path'); //引入路径
  if(!(name&&jxl&& xq&& jssbpz&& rl)){
    res.json({ code: 202, data: [], message: '数据错误,参数有问题' });
  }else{
    try {
      const data = fs.readFileSync(path.join(__dirname, '../source/goods.json'), 'utf8');
      // 解析JSON数据
      const jsonData = JSON.parse(data);
      const lastItem = jsonData.slice(-1)[0];
      const newdata = { id: (lastItem.id-0) + 1, name, jxl, xq, jssbpz, rl };
      try {
        const data1 = fs.writeFileSync(path.join(__dirname, '../source/goods.json'), JSON.stringify([...jsonData,newdata]));
        console.log('已写入');
      } catch (error) {
        console.log(err)
      }
      res.json({ code: 200, data: [], message: '数据保存成功' });
    } catch (err) {
      console.log(err)
      res.json({ code: 201, data: [], message: '数据错误,接口问题' });
    }
  }
 
  next();
});



/**
 * POST /api/v1/getClassroomById
 * @summary 获取教室
 * @description 获取教室
 * @tags [教室管理]
 * @produces application/json
 * @param {object} request.body.required - id
 * @return {object} 201 - 成功获取
 * @example request - 示例请求
 * {
 *   "id": 1
 * }
 * @example response - 示例响应
 * {
 *  data: 'ok'
 * }
 * @return {object} 201 - 异常
 * @consumes application/json
 * @return {object} 200 - 成功获取教室
 */
router.post('/api/v1/getClassroomById', async (req, res, next) => {
  const { id, name, jxl, xq, jssbpz, rl } = req.body;
  const fs = require('fs');
  const path = require('path'); //引入路径
  try {
    const data = fs.readFileSync(path.join(__dirname, '../source/goods.json'), 'utf8');
    // 解析JSON数据
    const jsonData = JSON.parse(data);
   
    let editData = jsonData.filter((exp) => {
      return exp.id === id;
    });
    if(editData.length === 1){
      res.json({ code: 200, data: editData[0], message: '数据获取成功' });
    }else{
      res.json({ code: 201, data: 'error', message: '找不到对应id数据' });

    }
  } catch (err) {
    res.json({ code: 201, data: 'error', message: '数据错误,接口问题' });
  }
  next();
});






/**
 * POST /api/v1/editClassroomById
 * @summary 编辑教室
 * @description 编辑教室
 * @tags [教室管理]
 * @produces application/json
 * @param {object} request.body.required - id
 * @return {object} 201 - 成功获取
 * @example request - 示例请求
 * {
 *   "id": 1,
 *   "name": "1324",
 *   "jxl": "北校区1号教学楼",
 *   "xq": "北校区",
 *   "jssbpz": "教室设备配置",
 *   "rl": "40"
 * }
 * @example response - 示例响应
 * {
 *  data: 'ok'
 * }
 * @return {object} 201 - 异常
 * @consumes application/json
 * @return {object} 200 - 成功获取教室
 */
router.post('/api/v1/editClassroomById', async (req, res, next) => {
  const { id, name, jxl, xq, jssbpz, rl } = req.body;
  const fs = require('fs');
  const path = require('path'); //引入路径
  try {
    const data = fs.readFileSync(path.join(__dirname, '../source/goods.json'), 'utf8');
    // 解析JSON数据
    const jsonData = JSON.parse(data);
    const filterData = jsonData.filter((exp) => {
      return exp.id !== id;
    });

    let editData = jsonData.filter((exp) => {
      return exp.id === id;
    });
    if(filterData.length === jsonData.length-1 ){

      editData = [
        ...filterData,
        { id, name, jxl, xq, jssbpz, rl}
      ]
     
      try {
        const data1 = fs.writeFileSync(path.join(__dirname, '../source/goods.json'), JSON.stringify(editData));
        console.log('已写入');
        res.json({ code: 200, data: 'ok', message: '数据编辑成功' });
      } catch (error) {
        res.json({ code: 201, data: 'error', message: '数据错误,接口问题' });
      }
    }else{

      res.json({ code: 201, data: 'error', message: '找不到对应id数据' });
       
    }
   
   
  } catch (err) {
    res.json({ code: 201, data: 'error', message: '数据错误,接口问题' });
  }
  next();
});



/**
 * POST /api/v1/delClassroomById
 * @summary 删除教室
 * @description 删除教室
 * @tags [教室管理]
 * @produces application/json
 * @param {object} request.body.required - 商品id
 * @return {object} 201 - 成功获取商品
 * @example request - 示例请求
 * {
 *   "id": 1
 * }
 * @example response - 示例响应
 * {
 *  data: 'ok'
 * }
 * @return {object} 201 - 异常
 * @consumes application/json
 * @return {object} 200 - 成功获取教室
 */
router.post('/api/v1/delClassroomById', async (req, res, next) => {
  const { id } = req.body;
  const fs = require('fs');
  const path = require('path'); //引入路径
  try {
    const data = fs.readFileSync(path.join(__dirname, '../source/goods.json'), 'utf8');
    // 解析JSON数据
    const jsonData = JSON.parse(data);
    const filterData = jsonData.filter((exp) => {
      return exp.id !== id;
    });
    try {
      const data1 = fs.writeFileSync(path.join(__dirname, '../source/goods.json'), JSON.stringify(filterData));
      console.log('已写入');
      res.json({ code: 200, data: 'ok', message: '数据获取成功' });
    } catch (error) {
      res.json({ code: 201, data: 'error', message: '数据错误,接口问题' });
    }
  } catch (err) {
    res.json({ code: 201, data: 'error', message: '数据错误,接口问题' });
  }
  next();
});


/**
 * POST /api/v1/getClassroomList
 * @summary 获取教室列表
 * @description 获取教室列表
 * @tags [教室管理]
 * @produces application/json
 * @example response - 示例响应
 * [
 *   {
 *    "id": 1,
 *    "name": "1316",
 *    "jxl": "南校区2号教学楼",
 *    "xq": "南校区",
 *    "jssbpz": "教室设备配置",
 *    "rl": "30"
 *  },
 *  {
 *    "id": 2,
 *    "name": "1317",
 *    "jxl": "南校区1号教学楼",
 *    "xq": "南校区",
 *    "jssbpz": "教室设备配置",
 *    "rl": "30"
 *  },
 * ]
 * @return {object} 201 - 异常
 * @consumes application/json
 * @return {object} 200 - 成功获取教室
 */
router.post('/api/v1/getClassroomList', async (req, res, next) => {
  const fs = require('fs');

  const path = require('path'); //引入路径
  try {
    const data = fs.readFileSync(path.join(__dirname, '../source/goods.json'), 'utf8');
    // 解析JSON数据
    const jsonData = JSON.parse(data);
    res.json({ code: 200, data: jsonData, message: '数据获取成功' });
  } catch (err) {
    res.json({ code: 201, data: [], message: '数据错误,接口问题' });
  }
  next();
});




/**
 * POST /api/v1/getClassroomByPage
 * @summary 获取教室列表
 * @description 获取教室列表
 * @tags [教室管理]
 * @produces application/json
 * @param {object} request.body.required - 商品id
 * @return {object} 201 - 成功获取商品
 * @example request - 示例请求
 * {
 *   "name": "1212",
 *   "size":1
 * }
 * @example response - 200 - success response example
 * {
 *  "page":{
 *    "size":1,
 *    "count":21
 *  },
 *  "list":[
 *   {
 *    "id": 1,
 *    "name": "1316",
 *    "jxl": "南校区2号教学楼",
 *    "xq": "南校区",
 *    "jssbpz": "教室设备配置",
 *    "rl": "30"
 *  },
 *  {
 *    "id": 2,
 *    "name": "1317",
 *    "jxl": "南校区1号教学楼",
 *    "xq": "南校区",
 *    "jssbpz": "教室设备配置",
 *    "rl": "30"
 *  }
 * ]
 * }
 * @consumes application/json
 * @return {object} 200 - 成功获取教室
 */
router.post('/api/v1/getClassroomByPage', async (req, res, next) => {

  // *    "id": 1,
  // *    "name": "1316",
  // *    "jxl": "南校区2号教学楼",
  // *    "xq": "南校区",
  // *    "jssbpz": "教室设备配置",
  // *    "rl": "30"

  const { name,jxl,xq,jssbpz,rl,size } = req.body;

  const fs = require('fs');

  const path = require('path'); //引入路径
  let warningText= '';

  try {
    if(!(size)){
      warningText += '翻页参数size不能为空'
      throw '翻页数据不对'
    }
    const data = fs.readFileSync(path.join(__dirname, '../source/goods.json'), 'utf8');
    // 解析JSON数据
    const jsonData = JSON.parse(data);
    let filterData = jsonData

    if(name){
      filterData = filterData.filter((exp) => {
        return exp.name.includes(name);
      });
    }
   
    if(jxl){
      filterData = filterData.filter((exp) => {
        return exp.jxl.includes(jxl);
      });
    }
    if(xq){
      filterData = filterData.filter((exp) => {
        return exp.xq.includes(xq);
      });
    }

    if(jssbpz){
      filterData = filterData.filter((exp) => {
        return exp.jssbpz.includes(jssbpz);
      });
    }

    if(rl){
      filterData = filterData.filter((exp) => {
        return exp.rl.includes(rl);
      });
    }
  
    res.json({ code: 200, data: {
      page:{
        size:size,
        count:filterData.length
      },
      list:filterData.slice(size*10-10,size*10)
    }, message: '数据获取成功' });
  } catch (err) {
    console.log(err)
    res.json({ code: 201, data: [], message: warningText?warningText:'数据错误,接口问题' });
  }
  next();
});


module.exports = router;
